package com.hc.conferencecheck.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hc.conferencecheck.entity.Conference;
import com.hc.conferencecheck.entity.ConferenceAddress;
import com.hc.conferencecheck.entity.ResultMsg;
import com.hc.conferencecheck.entity.ResultStatusCode;
import com.hc.conferencecheck.entity.entityDTO.ConferenceAppliedDTO;
import com.hc.conferencecheck.service.ConferenceService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/conferenceController")
public class ConferenceController {
    @Resource
    private ConferenceService conferenceServiceImpl;

    /**
     * 根据用户ID查询该用户所有某状态的申请记录
     */
    @RequestMapping("/selectConferenceInfoByConventionerId")
    public ResultMsg selectConferenceInfoByConventionerId(String param) {
        Map params = (Map) JSONObject.parseObject(param);
        Object isApprovedObj = params.get("isApproved");
        if (isApprovedObj == null) {
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "状态码不可为空");
        }
        Object conventionerIdObj = params.get("conventionerId");
        if (conventionerIdObj == null) {
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "用户ID参数不可为空");
        }
        ResultMsg resultMsg = conferenceServiceImpl.selectConferenceInfoByConventionerId(params);
        return resultMsg;
    }

    /**
     * 查询所有会议的所有审核还未通过的记录（审核未通过，审核中）
     */
    @RequestMapping("/selectAttendRecordNotApproved")
    public ResultMsg selectAttendRecordNotApproved(String param) {
        Map params = (Map) JSONObject.parseObject(param);
        List list = conferenceServiceImpl.selectAttendRecordNotApproved(params);
        return new ResultMsg(ResultStatusCode.OK.getCode(), ResultStatusCode.OK.getMsg(), list);
    }

    /**
     * 查询所有的会议对象的基础信息
     *
     * @return
     */
    @RequestMapping("/selectAllConference")
    public ResultMsg selectAllConference(String param) {
        Map params = (Map) JSONObject.parseObject(param);
        //分页参数的非空判断
        Object pageNumberObj = params.get("pageNumber");
        if (pageNumberObj == null) {
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "当前页数不可为空");
        }
        ResultMsg resultMsg = conferenceServiceImpl.selectAllConference(params);
        return resultMsg;
    }

    /**
     * 会议后台管理页面查询会议详情
     *
     * @return
     */
    @RequestMapping("/selectConferenceInfo")
    public ResultMsg selectConferenceInfo(String param) {
        Map params = (Map) JSONObject.parseObject(param);
        Object conferenceIdObj = params.get("conferenceId");
        if (conferenceIdObj == null) {
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "会议ID不可为空");
        }

        ResultMsg resultMsg = conferenceServiceImpl.selectConferenceInfo(params);

        return resultMsg;
    }

    /**
     * 查询当前用户所有可签到会议
     */
    @RequestMapping("/selectBranchAddressAllowSign")
    public ResultMsg selectBranchAddressAllowSign(String param) {
        Map params = (Map) JSONObject.parseObject(param);
        Object conventionerIdObj = params.get("conventionerId");
        if (conventionerIdObj == null) {
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "用户ID不可为空");
        }
        int conventionerId = Integer.parseInt(conventionerIdObj.toString());

        ResultMsg resultMsg = conferenceServiceImpl.selectBranchAddressAllowSign(conventionerId);

        return resultMsg;
    }


    /**
     * “我的会议申请”模块中的会议详情
     */
    @RequestMapping("/selectConferenceByIds")
    public ResultMsg selectConferenceByIds(String param) {
        Map params = (Map) JSONObject.parseObject(param);
        Object conferenceIdObj = params.get("conferenceId");
        if (conferenceIdObj == null) {
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "会议ID不可为空");
        }
        Object conventionerIdObj = params.get("conventionerId");
        if (conventionerIdObj == null) {
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "用户ID不可为空");
        }

        ResultMsg resultMsg = conferenceServiceImpl.selectConferenceByIds(params);

        return resultMsg;
    }

    /**
     * 查询当前用户所有已申请会议
     */
    @RequestMapping("/selectConferenceApplied")
    public ResultMsg selectConferenceApplied(String param) {
        Map params = (Map) JSONObject.parseObject(param);
        Object conventionerIdObj = params.get("conventionerId");
        if (conventionerIdObj == null) {
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "参数不可为空");
        }
        int conventionerId = Integer.parseInt(conventionerIdObj.toString());
        List<ConferenceAppliedDTO> conferenceAppliedDTOS = conferenceServiceImpl.selectConferenceApplied(conventionerId);

        if (conferenceAppliedDTOS != null && conferenceAppliedDTOS.size() > 0) {
            return new ResultMsg(ResultStatusCode.OK.getCode(), ResultStatusCode.OK.getMsg(), conferenceAppliedDTOS);
        } else {
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "没有已申请会议");
        }
    }

    /**
     * 修改会议对象
     */
    @RequestMapping("/editConference")
    public ResultMsg editConference(String conferenceJson, String addressListJson) {
        //装换json为对象
        Conference conference = JSONObject.parseObject(conferenceJson, Conference.class);

        List<ConferenceAddress> addressList = JSONArray.parseArray(addressListJson, ConferenceAddress.class);

        ResultMsg resultMsg = conferenceServiceImpl.editConference(conference, addressList);
        return resultMsg;
    }

    /**
     * 删除会议对象
     */
    @RequestMapping("/deleteConferenceByPrimaryKey")
    public ResultMsg deleteConferenceByPrimaryKey(String param) {
        Map params = (Map) JSONObject.parseObject(param);
        Object conferenceIdObj = params.get("conferenceId");
        if (conferenceIdObj == null) {
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "参数不可为空");
        }
        int conferenceId = Integer.parseInt(conferenceIdObj.toString());
        ResultMsg resultMsg = conferenceServiceImpl.deleteConferenceByPrimaryKey(conferenceId);
        return resultMsg;
    }

    /**
     * 添加会议对象
     */
    @RequestMapping("/createConference")
    public ResultMsg createConference(String conferenceJson, String addressListJson, String isEdit, HttpServletRequest req) {
        //获取图片文件
        MultipartHttpServletRequest request = (MultipartHttpServletRequest) req;
        MultipartFile file = request.getFile("file");
        //装换json为对象
        Conference conference = JSONObject.parseObject(conferenceJson, Conference.class);

        List<ConferenceAddress> addressList = JSONArray.parseArray(addressListJson, ConferenceAddress.class);

        if (isEdit == null || "".equals(isEdit)) {
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "判断参数为空");
        } else {
            //判断是否是添加操作
            if ("1".equals(isEdit)) {
                //说明是修改操作，先执行删除操作
                Integer conferenceId = conference.getId();
                ResultMsg resultMsg = conferenceServiceImpl.deleteConferenceByPrimaryKey(conferenceId);
                if (resultMsg.getCode() != 200) {
                    return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "删除元数据失败");
                } else {
                    ResultMsg resultMsg02 = conferenceServiceImpl.createConference02(conference, addressList, file, conferenceId);
                    return resultMsg02;
                }
            }
        }
        //执行添加操作
        ResultMsg resultMsg = conferenceServiceImpl.createConference(conference, addressList, file);
        return resultMsg;
    }

    /**
     * 申请报名会议接口
     */
    @RequestMapping("/applyConference")
    public ResultMsg applyConference(String param) {
        Map params = (Map) JSONObject.parseObject(param);
        ResultMsg resultMsg = conferenceServiceImpl.applyConference(params);
        return resultMsg;
    }

    /**
     * 根据主键查询指定的会议对象
     *
     * @return
     */
    @RequestMapping("/selectConferenceByPrimaryKey")
    public ResultMsg selectConferenceByPrimaryKey(String param) {
        Map params = (Map) JSONObject.parseObject(param);
        ResultMsg resultMsg = conferenceServiceImpl.selectConferenceByPrimaryKey(params);
        return resultMsg;
    }


    /**
     * 查询当前用户所有的有效会议对象
     */
    @RequestMapping("/selectAllConferences")
    public ResultMsg selectAllConferences(String param,HttpServletRequest request) {
        HttpSession session = request.getSession();
        Map params = (Map) JSONObject.parseObject(param);
        ResultMsg resultMsg = conferenceServiceImpl.selectAllConferences(params);
        return resultMsg;
    }

}
